package org.litu.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.litu.app.dao.SysConfigsMapper;
import org.litu.app.entity.system.SysConfigs;
import org.litu.app.service.ISysConfigsService;
import org.litu.base.service.impl.BaseServiceImpl;
import org.litu.core.exception.LtParamException;
import org.litu.core.login.UserInfo;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ltgk
 * @since 2018-10-29
 */
@Service
public class SysConfigsServiceImpl extends BaseServiceImpl<SysConfigsMapper, SysConfigs> implements ISysConfigsService {

    /**
     * 设置配置的值
     *
     * @param code  配置的code信息。
     * @param value 配置的值信息
     * @return
     */
    @Override
    public boolean setConfigValue(String code, String value, UserInfo user) {
        LambdaQueryWrapper<SysConfigs> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysConfigs::getCode, code);
        SysConfigs sysConfigs = getOne(queryWrapper);
        if (sysConfigs == null) {
            throw new LtParamException("未找到配置项" + code);
        }

        sysConfigs.setValue(value);
        sysConfigs.setModifyTime(new Date());
        if (user != null) {
            sysConfigs.setModifyBy(user.getId());
        }
        return updateById(sysConfigs);
    }

    /**
     * 根据配置code获取value
     *
     * @param code 配置的code值
     * @return
     */
    @Override
    public String getConfigValue(String code) {
        LambdaQueryWrapper<SysConfigs> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysConfigs::getCode, code);
        SysConfigs sysConfigs = getOne(queryWrapper);
        if (sysConfigs != null) {
            return sysConfigs.getValue();
        }
        return "";
    }
}
